package  com.globagames.isometric.core.view.actions
{
	import com.globagames.isometric.core.event.IsoMapMouseEvent;
	import com.globagames.isometric.core.model.Block;
	import com.globagames.isometric.core.model.IsoEngineModelLocator;
	import com.globagames.isometric.core.model.IsoTile;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.core.UIComponent;
	import mx.managers.CursorManager;
	
	import org.cove.ape.RectangleParticle;
			
	public class DragMapAction extends UIComponent implements IAction {
		private var model:IsoEngineModelLocator = IsoEngineModelLocator.getInstance();
		public function DragMapAction() {			
		}
		override protected function createChildren():void{
			x = 0;
			y = 0;
			super.createChildren();			
			this.width = model.screenWidth;
			this.height = model.screenHeight;			
		}
		
		public function rollOverHandler(event:Event):void {        	
	   		   			   		
	   			
        }
     
        
        
		public function mouseMoveHandler(event:MouseEvent):void {
			if( model.draggingMapIsOn ) {
				model.isoMap.regNewX = model.isoMap.mouseX;
				model.isoMap.regNewY = model.isoMap.mouseY;				
				model.isoMap.invalidateProperties();
				event.updateAfterEvent();
			}
		}
		public function mouseDownHandler(event:MouseEvent):void {	
			if( model.mouseActionIsOn == false ) {					
				var position:IsoMapMouseEvent = model.isoMap.getCurrentMousePosition(model.isoMap.mouseX,model.isoMap.mouseY);
				if( position.isoTile && model.autoScrollingIsOn == false) {								
					model.draggingMapIsOn = true;
					if( CursorManager.currentCursorID == model.cursorID ) {	
						CursorManager.setCursor(model.dragCursorSymbol);	
						model.handCursorID = CursorManager.currentCursorID;
					}		
					model.isoMap.regX = model.isoMap.mouseX;
					model.isoMap.regY = model.isoMap.mouseY;
					this.systemManager.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);						            						            	
	   			} 
  			}
		}	
		public function mouseUpHandler(event:MouseEvent):void {
			if( model.draggingMapIsOn ) {
				this.systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);				
				if( CursorManager.currentCursorID != model.cursorID ) {	
					CursorManager.removeCursor(CursorManager.currentCursorID);
				}	
				model.draggingMapIsOn = false;
				model.isoMap.doRetrace = true;
				model.isoMap.invalidateProperties();
				event.updateAfterEvent();
			}			
		}
		public function attach():void {	
			trace("attach");	
				
			this.systemManager.addEventListener(MouseEvent.ROLL_OVER , rollOverHandler);
			this.systemManager.addEventListener(MouseEvent.ROLL_OUT , mouseUpHandler);
			this.systemManager.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);			
			this.systemManager.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		public function detach():void {					
			if( model.isoMap ) {				
				this.systemManager.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
				this.systemManager.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
				this.systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
				if( CursorManager.currentCursorID == model.handCursorID ) {	
					CursorManager.removeCursor(CursorManager.currentCursorID);
				}
			}
						
		}

	}
}